package com.hackintosh.observer.school;

import java.util.ArrayList;
import java.util.List;

/**
 * @description：目标类：事件源，铃
 * @author：twislyn
 * @date：Created in 2021/05/27
 * @modified By：
 * @version: 1.0
 */
public class BellEventSource {
    /**
     * 监听器容器
     */
    private List<BellEventListener> listener;

    public BellEventSource() {
        listener = new ArrayList<>();
    }

    /**
     * 给事件源绑定监听器
     *
     * @param person
     */
    public void add(BellEventListener person) {
        listener.add(person);
    }

    public void ring(Boolean sound) {
        String type = sound ? "上课铃" : "下课铃";
        System.out.println(type + "响！");
        RingEvent event = new RingEvent(this, sound);
        notice(event);
    }

    private void notice(RingEvent event) {
        listener.forEach(person -> person.heardBell(event));
    }
}
